package com.app.newiptv.adaper;

import android.os.Handler;
import android.os.Message;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.app.newiptv.R;
import com.app.newiptv.base.MyApplication;
import com.app.newiptv.presenter.AllChannelsPresenter;
import com.app.newiptv.utils.APPLog;


/**
 * Created by Shinelon on 2017/9/15.
 */

public class ChanelAllAdapter extends RecyclerView.Adapter<ChanelAllAdapter.MainRecycleViewHolder> {
    private AllChannelsPresenter presenter;
    private int count;
    private int position;
    private RecyclerView parentView;
    private MainRecycleViewHolder currentHolder;
    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            for (int i = 0; i < parentView.getChildCount(); i++) {
                View childView = parentView.getChildAt(i);
                int position = parentView.getChildLayoutPosition(childView);
                MainRecycleViewHolder viewHolder = (MainRecycleViewHolder) parentView.getChildViewHolder(childView);
                APPLog.printInfo("TVodViewHolder:" + position);
                if (position == ChanelAllAdapter.this.position) {
                    currentHolder = viewHolder;
                    currentHolder.onFocusChange(true);
                    APPLog.printInfo("this.position:" + ChanelAllAdapter.this.position);
                    break;
                }
            }
        }
    };
    public ChanelAllAdapter(AllChannelsPresenter presenter) {
        this.presenter = presenter;
    }

    public void setCount(int count) {
        this.count = count;
    }

    @Override
    public MainRecycleViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_channels_view, null, false);
        MainRecycleViewHolder mainRecycleViewHolder = new MainRecycleViewHolder(view);
        if (parentView == null) {
            parentView = (RecyclerView) parent;
            parentView.addOnScrollListener(new ChannelScrollListener());
        }
        return mainRecycleViewHolder;
    }

    @Override
    public void onBindViewHolder(MainRecycleViewHolder holder, int position) {
        presenter.onBindChannel(holder, position);
        holder.setPositon(position);
    }

    @Override
    public int getItemCount() {
        return count;
    }

    public int selectedNext() {
        // if (position < count - 1) {
        unselected();
        position += 1;
        selected();
        // }
        return position;
    }

    public int selectedPre() {
        // if (position > 0) {
        unselected();
        position -= 1;
        selected();
        // }
        return position;
    }

    public void selected() {
        if (parentView == null) {
            return;
        }
        if (position >= count) {
            parentView.smoothScrollToPosition(0);
            this.position = 0;
        } else if (position < 0) {
            parentView.smoothScrollToPosition(count - 1);
            this.position = count - 1;
        } else {
            parentView.smoothScrollToPosition(this.position);
        }
        handler.sendEmptyMessageDelayed(0, 200);
        //for (int i = 0; i < parentView.getChildCount(); i++) {
        //    View childView = parentView.getChildAt(i);
        //    int position = parentView.getChildLayoutPosition(childView);
        //    MainRecycleViewHolder viewHolder = (MainRecycleViewHolder) parentView.getChildViewHolder(childView);
        //    APPLog.printInfo("TVodViewHolder:" + position);
        //    if (position == this.position) {
        //        currentHolder = viewHolder;
        //        viewHolder.onFocusChange(true);
        //        APPLog.printInfo("this.position:" + this.position);
        //        break;
        //    }
        //}
    }

    public void unselected() {
        if (currentHolder != null) {
            currentHolder.onFocusChange(false);
            currentHolder = null;
            APPLog.printInfo("channelAdapter :" + "unselected()" + position);
        }
    }
    public void onClick(){
        if (currentHolder != null){
            currentHolder.onClick();
        }
    }

    public class MainRecycleViewHolder extends RecyclerView.ViewHolder implements AllChannelsPresenter.SencondViewHolder {

        private TextView mainchanel_text;
        public View itemView;
        public int positon;

        public void setPositon(int positon) {
            this.positon = positon;
        }

        public MainRecycleViewHolder(View itemView) {
            super(itemView);
            this.itemView = itemView;
            mainchanel_text = (TextView) itemView.findViewById(R.id.mainchanel_text);
        }

        public void onFocusChange(boolean hasFocus) {
            if (hasFocus) {
                itemView.setBackgroundResource(R.mipmap.dise_left);
                presenter.loadProgram(positon);
            } else {
                itemView.setBackgroundResource(0);
            }
        }
        public void onClick(){
            presenter.toPlayPosition(positon);
        }

        @Override
        public void setChannel(String name) {
            mainchanel_text.setTypeface(MyApplication.face);
            mainchanel_text.setText(name);
        }
    }

    class ChannelScrollListener extends RecyclerView.OnScrollListener {
        @Override
        public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
            if (newState == RecyclerView.SCROLL_STATE_IDLE) {
                //unselected();
                //selected();
            }
        }
    }
}
